﻿using B.S.Material.Domain.BOM;
using B.S.Material.ErrorCode;
using B.S.Material.Wirth.Api.Applications.Command.MaterialsChildCommand;
using MediatR;
using SqlSugar;

namespace B.S.Material.Wirth.Api.Applications.Handler.MaterialsChildHandler
{
    /// <summary>
    /// 批量删除物料清单子表
    /// </summary>
    public class PLDelMaterialsChildHandler : IRequestHandler<PLDelMaterialsChildCommand, ApiResult<int>>
    {
        private readonly ISqlSugarClient db;
        public PLDelMaterialsChildHandler(ISqlSugarClient db)
        {
            this.db = db;
        }

        public async Task<ApiResult<int>> Handle(PLDelMaterialsChildCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            List<MaterialsChild> list = new List<MaterialsChild>();
            foreach (var item in request.ids)
            {
                var res = db.Queryable<MaterialsChild>().First(x => x.ChildID == item);
                res.IsDel = true;
                list.Add(res);
            }
            result.data = await db.Updateable(list).ExecuteCommandAsync();
            if (result.data > 0)
            {
                result.Code = ApiEnum.删除成功;
                result.MAG = ApiEnum.删除成功.ToString();
            }
            else
            {
                result.Code = ApiEnum.删除失败;
                result.MAG = ApiEnum.删除失败.ToString();
            }
            return result;
        }
    }
}
